package com.own.business.push.controller;

import com.own.aop.login.LoginIgnore;
import com.own.business.push.core.base.BaseSaveRegistrationIdMethod;
import com.own.business.push.core.config.PushCoreConfig;
import com.own.business.push.core.model.RegisterRegistrationIdModel;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.component.log.operation.aop.LogOperationModuleAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;

/**
 * UserPushCoreController
 *
 * @author chenxueli
 * @date 2023-11-14 09:25:00
 */
@Tag(name = "推送中心" + ConstantBeanSuffix.USER_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@LogOperationModuleAop("推送中心" + ConstantBeanSuffix.USER_CLIENT)
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.USER_CLIENT + "push/core")
public class UserPushCoreController extends BaseController {

    @Resource
    private BaseSaveRegistrationIdMethod baseSaveRegistrationIdMethod;
    @Resource
    private PushCoreConfig pushCoreConfig;

    @LogOperationAop
    @Operation(summary = "注册")
    @PostMapping("/register")
    public ResultModel<Boolean> register(
            @RequestBody RegisterRegistrationIdModel form
    ) {
        var loginUser = SessionUserUtil.getLoginUser();
        form.setUserId(loginUser.userId());
        baseSaveRegistrationIdMethod.save(form);
        return R.success(true);
    }

    @LogOperationAop
    @Operation(summary = "注销")
    @PostMapping("/logout")
    public ResultModel<Boolean> logout() {
        var loginUser = SessionUserUtil.getLoginUser();
        baseSaveRegistrationIdMethod.removeByUserId(loginUser.userId());
        return R.success(true);
    }

    @LoginIgnore
    @LogOperationAop
    @Operation(summary = "获取所有可用的名称")
    @GetMapping("/names")
    public ResultModel<Set<String>> names() {
        var names = pushCoreConfig.names();
        return R.success(names);
    }

}
